import OpenGL.GL as gl
import numpy as np
import glm
import pygl

class AxesHelper:
    def __init__(self, length=1.0, width=1.0) -> None:
        self.program = pygl.ProgramVF("common/shaders/axes.vs", 
                                      "common/shaders/axes.fs")
        self.axis_length = length
        self.axis_width = width
        vertices = np.array([
            0.0, 0.0, 0.0, self.axis_length, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, self.axis_length, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, self.axis_length
        ], dtype=gl.GLfloat)
        colors = np.array([ 
            1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 1.0, 0.0, 0.0, 1.0
        ], dtype=gl.GLfloat)
        self.vao = pygl.VertexArrayObject()
        self.position_vbo = pygl.VertexBufferObject(vertices)
        self.color_vbo = pygl.VertexBufferObject(colors)
        position_binding_point = 0
        color_binding_point = 1
        self.vao.setVertexBuffer(self.position_vbo, position_binding_point, 
                                 0, 3 * gl.sizeof(gl.GLfloat))
        self.vao.setVertexBuffer(self.color_vbo, color_binding_point, 0, 
                                 3 * gl.sizeof(gl.GLfloat))
        attribute_position = pygl.VertexAttribute("position", 0, 3, 
                                            gl.GL_FLOAT, gl.GL_FALSE, 0)
        attribute_color = pygl.VertexAttribute("color",1, 3, 
                                               gl.GL_FLOAT, gl.GL_FALSE, 0)
        self.vao.setVertexAttribute(position_binding_point, attribute_position)
        self.vao.setVertexAttribute(color_binding_point, attribute_color)
        self.rotation = glm.vec3(0.0, 0.0, 0.0)
        self.position = glm.vec3(0.0, 0.0, 0.0)
        self.scale = glm.vec3(1.0, 1.0, 1.0)
        self.updateModelMatrix()

    def render(self, view_matrix: glm.mat4, projection_matrix: glm.mat4) -> None:
        self.program.use()
        self.vao.bind()
        self.program.setUniformMatrix4fv(0, self.model_matrix)
        self.program.setUniformMatrix4fv(1, view_matrix)
        self.program.setUniformMatrix4fv(2, projection_matrix)
        gl.glLineWidth(self.axis_width)
        gl.glDrawArrays(gl.GL_LINES, 0, 6)

    def delete(self)->None:
        self.vao.delete()
        self.color_vbo.delete()
        self.position_vbo.delete()
        self.program.delete()

    def updateModelMatrix(self)->None:
        scale_matrix = glm.scale(glm.mat4(1.0), self.scale)
        rotation_matrix = glm.rotate(glm.mat4(1.0), glm.radians(self.rotation.x), 
                                     glm.vec3(1.0, 0.0, 0.0))
        rotation_matrix = glm.rotate(rotation_matrix, 
                        glm.radians(self.rotation.y), glm.vec3(0.0, 1.0, 0.0))
        rotation_matrix = glm.rotate(rotation_matrix, 
                        glm.radians(self.rotation.z), glm.vec3(0.0, 0.0, 1.0))
        translation_matrix = glm.translate(self.position)
        self.model_matrix = translation_matrix * rotation_matrix * scale_matrix

